/**
 * 打印报工信息
 * @param {Object} data - 报工数据
 */
export async function printReportWork(data) {
  // 动态导入qrcodejs2库
  const QRCode = (await import('qrcodejs2')).default;
  
  // 创建打印容器
  const printContainer = document.createElement('div');
  printContainer.style.width = '10cm';
  printContainer.style.height = '7cm';
  printContainer.style.padding = '0';
  printContainer.style.border = '1px solid #000';
  printContainer.style.fontFamily = 'Arial, sans-serif';
  printContainer.style.position = 'relative';
  printContainer.style.background = 'white';
  printContainer.style.margin = '0 auto';
  printContainer.style.overflow = 'hidden';
  
  // 创建表格展示工单信息 - 按照图片格式重新布局
  const table = document.createElement('table');
  table.style.width = '100%';
  table.style.borderCollapse = 'collapse';
  table.style.fontSize = '9px'; // 微调字体大小以确保内容完整显示
  table.style.lineHeight = '1.2'; // 调整行高确保一行完整显示
  table.style.border = 'none';
  
  // 创建通用的设置单元格样式的函数
  function setCellStyle(cell, options = {}) {
    const defaults = {
      border: '1px solid #000',
      padding: '2px 3px', // 增加内边距以确保内容完整显示
      textAlign: 'center',
      verticalAlign: 'middle',
      width: null
    };
    
    const settings = { ...defaults, ...options };
    
    cell.style.border = settings.border;
    cell.style.padding = settings.padding;
    cell.style.textAlign = settings.textAlign;
    cell.style.verticalAlign = settings.verticalAlign;
    if (settings.width) {
      cell.style.width = settings.width;
    }
  }
  
  // 第一行：品名、C-01、日期时间
  const row1 = document.createElement('tr');
  
  const nameLabel = document.createElement('td');
  nameLabel.textContent = '品名';
  setCellStyle(nameLabel, { width: '15%' });
  row1.appendChild(nameLabel);
  
  const nameValue = document.createElement('td');
  nameValue.textContent = data.materialName || data.materialNo || '-';
  setCellStyle(nameValue, { width: '35%', textAlign: 'left' });
  row1.appendChild(nameValue);
  
  const batchIdCell = document.createElement('td');
  batchIdCell.textContent = '报工时间';
  setCellStyle(batchIdCell, { width: '15%' });
  row1.appendChild(batchIdCell);
  
  const dateTimeCell = document.createElement('td');
  // 格式化日期时间为更紧凑的格式
  const dateStr = data.reportTime ? new Date(data.reportTime).toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  }) : new Date().toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  });
  dateTimeCell.textContent = dateStr;
  setCellStyle(dateTimeCell, { width: '35%' });
  row1.appendChild(dateTimeCell);
  
  table.appendChild(row1);
  
  // 第二行：机台、数量
  const row2 = document.createElement('tr');
  
  const machineLabel = document.createElement('td');
  machineLabel.textContent = '机台';
  setCellStyle(machineLabel, { width: '15%' });
  row2.appendChild(machineLabel);
  
  const machineValue = document.createElement('td');
  // 简化机台显示，避免内容过长
  const deviceText = data.deviceName ? data.deviceName : (data.deviceCode ? data.deviceCode : '-');
  machineValue.textContent = deviceText;
  setCellStyle(machineValue, { width: '35%', textAlign: 'left' });
  row2.appendChild(machineValue);
  
  const quantityLabel = document.createElement('td');
  quantityLabel.textContent = '数量';
  setCellStyle(quantityLabel, { width: '15%' });
  row2.appendChild(quantityLabel);
  
  const quantityValue = document.createElement('td');
  quantityValue.textContent = data.reportQuantity || '0';
  quantityValue.style.fontSize = '14px'; // 数量字体稍大
  setCellStyle(quantityValue, { width: '35%' });
  row2.appendChild(quantityValue);
  
  table.appendChild(row2);
  
  // 第三行：追溯码、二维码
  const row3 = document.createElement('tr');
  
  const traceLabel = document.createElement('td');
  traceLabel.textContent = '追溯码';
  setCellStyle(traceLabel, { width: '15%', verticalAlign: 'top' });
  row3.appendChild(traceLabel);
  
  const traceValue = document.createElement('td');
  traceValue.colSpan = '2';
  // 处理长批次号，保持整洁
  const batchNoText = data.batchNo ? (data.batchNo.length > 25 ? data.batchNo.substring(0, 25) + '****' : data.batchNo) : '-';
  traceValue.textContent = batchNoText;
  setCellStyle(traceValue, { textAlign: 'left', verticalAlign: 'top' });
  row3.appendChild(traceValue);
  
  // 二维码单元格
  const qrCodeCell = document.createElement('td');
  qrCodeCell.style.height = '2.5cm'; // 固定二维码行高
  setCellStyle(qrCodeCell, { width: '35%', padding: '3px' });
  
  // 创建二维码容器
  const qrCodeContainer = document.createElement('div');
  qrCodeContainer.id = 'qr-code-container';
  qrCodeContainer.style.width = '1.8cm';
  qrCodeContainer.style.height = '1.8cm';
  qrCodeContainer.style.margin = '0 auto';
  qrCodeContainer.style.display = 'flex';
  qrCodeContainer.style.alignItems = 'center';
  qrCodeContainer.style.justifyContent = 'center';
  qrCodeCell.appendChild(qrCodeContainer);
  row3.appendChild(qrCodeCell);
  
  table.appendChild(row3);
  
  // 第四行：物料二维码、工序名称
  const row4 = document.createElement('tr');
  
  const materialQrLabel = document.createElement('td');
  materialQrLabel.textContent = '物料二维码';
  setCellStyle(materialQrLabel, { width: '15%' });
  row4.appendChild(materialQrLabel);
  
  const materialQrValue = document.createElement('td');
  materialQrValue.textContent = data.batchNo || '-';
  setCellStyle(materialQrValue, { width: '35%', textAlign: 'left' });
  row4.appendChild(materialQrValue);
  
  const u3Cell = document.createElement('td');
  u3Cell.textContent = data.processName || '-'; // 使用工序名称
  u3Cell.style.fontSize = '20px';
  u3Cell.style.fontWeight = 'bold';
  setCellStyle(u3Cell, { width: '15%' });
  row4.appendChild(u3Cell);
  
  const emptyCell1 = document.createElement('td');
  setCellStyle(emptyCell1, { width: '35%' });
  row4.appendChild(emptyCell1);
  
  table.appendChild(row4);
  
  // 第五行：工单号
  const row5 = document.createElement('tr');
  
  const planLabel = document.createElement('td');
  planLabel.textContent = '报工号';
  setCellStyle(planLabel, { width: '15%' });
  row5.appendChild(planLabel);
  
  const planValue = document.createElement('td');
  planValue.textContent = data.reportWorkNo || '-';
  setCellStyle(planValue, { width: '35%', textAlign: 'left' });
  row5.appendChild(planValue);
  
  const emptyCell2 = document.createElement('td');
  emptyCell2.colSpan = '2';
  setCellStyle(emptyCell2);
  row5.appendChild(emptyCell2);
  
  table.appendChild(row5);
  
  // 第六行：报工号
  const row6 = document.createElement('tr');
  
  const statusLabel = document.createElement('td');
  statusLabel.textContent = '工单号';
  setCellStyle(statusLabel, { width: '15%' });
  row6.appendChild(statusLabel);
  
  const statusValue = document.createElement('td');
  statusValue.colSpan = '3';
  statusValue.textContent = data.workOrderNo || '-'; // 显示报工号
  setCellStyle(statusValue, { textAlign: 'left' });
  row6.appendChild(statusValue);
  
  table.appendChild(row6);
  
  // 第七行：报工人、QA盖章
  const row7 = document.createElement('tr');
  
  const reporterLabel = document.createElement('td');
  reporterLabel.textContent = '报工人';
  setCellStyle(reporterLabel, { width: '15%' });
  row7.appendChild(reporterLabel);
  
  const reporterValue = document.createElement('td');
  reporterValue.textContent = data.reportUserName || '-';
  setCellStyle(reporterValue, { width: '35%', textAlign: 'left' });
  row7.appendChild(reporterValue);
  
  const qaLabel = document.createElement('td');
  qaLabel.textContent = '';
  setCellStyle(qaLabel, { width: '15%' });
  row7.appendChild(qaLabel);
  
  const qaValue = document.createElement('td');
  qaValue.textContent = '';
  setCellStyle(qaValue, { width: '35%' });
  row7.appendChild(qaValue);
  
  table.appendChild(row7);
  
  // 第八行：备注
  const row8 = document.createElement('tr');
  
  const remarkLabel = document.createElement('td');
  remarkLabel.textContent = '备注';
  setCellStyle(remarkLabel, { width: '15%' });
  row8.appendChild(remarkLabel);
  
  const remarkValue = document.createElement('td');
  remarkValue.colSpan = '3';
  remarkValue.textContent = data.remark || '';
  setCellStyle(remarkValue, { textAlign: 'left' });
  row8.appendChild(remarkValue);
  
  table.appendChild(row8);
  
  printContainer.appendChild(table);
  
  // 设置容器ID和可见性
  printContainer.id = 'print-content';
  printContainer.style.visibility = 'visible';
  
  // 将容器添加到body
  document.body.appendChild(printContainer);
  
  // 生成二维码
  try {
    const qrContent = data.batchNo || '';
    new QRCode(qrCodeContainer, {
      text: qrContent,
      width: 72,
      height: 72,
      colorDark: '#000000',
      colorLight: '#ffffff',
      correctLevel: QRCode.CorrectLevel.H
    });
  } catch (err) {
    console.error('生成二维码失败:', err);
    // 生成失败时显示批次号作为备选
    qrCodeContainer.textContent = data.batchNo || '无批次号';
    qrCodeContainer.style.fontSize = '8px';
  }
  
  // 打印样式 - 优化布局和样式
  const style = document.createElement('style');
  style.textContent = `
    /* 确保打印前在屏幕上也能正常预览 */
    #print-content {
      max-width: 10cm;
      max-height: 7cm;
    }
    
    @media print {
      * {
        box-sizing: border-box !important;
      }
      
      body {
        margin: 0 !important;
        padding: 0 !important;
        background: white !important;
        font-family: Arial, sans-serif !important;
      }
      
      /* 隐藏所有非打印内容 */
      body > *:not(#print-content) {
        display: none !important;
      }
      
      /* 确保打印内容正确显示 */
      #print-content {
        display: block !important;
        visibility: visible !important;
        width: 10cm !important;
        height: 7cm !important;
        margin: 0 !important;
        padding: 0 !important;
        position: absolute !important;
        top: 0 !important;
        left: 0 !important;
      }
      
      /* 确保表格正确显示 */
      #print-content table {
        width: 100% !important;
        height: 100% !important;
        border-collapse: collapse !important;
        border: none !important;
      }
      
      #print-content td {
        border: 1px solid #000 !important;
        padding: 1px 2px !important;
        font-size: 10px !important;
      }
      
      /* 确保二维码正确显示 */
      #qr-code-container {
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
      }
      
      #qr-code-container img {
        max-width: 100% !important;
        max-height: 100% !important;
      }
      
      /* 页面设置 */
      @page {
        size: 10cm 7cm;
        margin: 0;
        padding: 0;
      }
    }
  `;
  document.head.appendChild(style);
  
  // 执行打印
  setTimeout(() => {
    window.print();
    
    // 清理
    setTimeout(() => {
      try {
        document.body.removeChild(printContainer);
      } catch (e) {
        console.log('容器已移除');
      }
      try {
        document.head.removeChild(style);
      } catch (e) {
        console.log('样式已移除');
      }
    }, 100);
  }, 100);
}
